using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FBPChaos
{
	public class GraphicsEngine
	{
		int itemsToDraw;
        Texture2D cursorTexture;
        Texture2D wizardTexture;
        SpriteBatch spriteBatch;
        GraphicsDeviceManager graphics;
        ContentManager content;
        GraphicsDevice graphicsDevice;
        //TerrainGrid terrainGrid;
        List<Texture2D> monsterTextures;
        
        SpriteFont font;
        SpriteFont smallFont;
        
        ShowText messages;
        MainMenu mainMenu;
        
        const int XOffset = 20; // 640 - 40*15 = 40, 40/2 = 20
        const int YOffset = 0; //480 - 40*12 = 0
        const int cellSize = 40;

        public GraphicsEngine(GraphicsDeviceManager graphicsDeviceManager, ContentManager contentManager, GraphicsDevice currentGraphicsDevice, ShowText textMessages, MainMenu menu)
        {
            itemsToDraw = 0;
            graphicsDevice = currentGraphicsDevice;
            graphics = graphicsDeviceManager;
            content = contentManager;
            //terrainGrid = terrain;
            monsterTextures = new List<Texture2D>();
            messages = textMessages;
            mainMenu = menu;
        }

        public void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(graphicsDevice);
            cursorTexture = content.Load<Texture2D>("cursor");
            wizardTexture = content.Load<Texture2D>("wizard");
            font = content.Load<SpriteFont>("MainFont");
            smallFont = content.Load<SpriteFont>("SmallFont");
        }

        public void LoadMonsters(List<Monster> monsters)
        {
            try
            {
                foreach (Monster monster in monsters)
                    monsterTextures.Add(content.Load<Texture2D>(monster.MonsterName + ".png"));
            }
            catch (Exception e)
            {

            }

        }

        public void LoadWizards()
        {

        }

        public void Draw(Wizard[] wizards, List<Monster> monsters, Cursor cursor)
        {
            graphicsDevice.Clear(Color.Black);
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            drawMessages();
            if (mainMenu.Active)
            {
                drawMainMenu();   
            }
            else
            {
                //messages.addText("drawWizards call");
                drawWizards(wizards);
                if (cursor.Active)
                    drawCursor(cursor);
            }

            //spriteBatch.Draw(cursorTexture, cursorPosition, Color.White);
            spriteBatch.End();

        }

        private void drawMessages()
        {
            int line = 0;
            int startLine;
            //max 20 lines on screen at once
            if (messages.Length >= 20)
                startLine = messages.Length - 20;
            else
                startLine = 0;
            for (int counter = startLine; counter < messages.Length; counter++)
            {
                spriteBatch.DrawString(smallFont, messages.CurrentText[counter], new Vector2(20, line), Color.DarkGoldenrod);
                line = line + 20;
            }
        }


        private void drawMainMenu()
        {
            int spacing = 10;
            Vector2 stringSize;
            //if menu position = "centre" count number of strings, add up total heights add spacing * num of strings -1
            //if menu position = "normal" make indent 15% and start 20% from top of screen
            int height = graphicsDevice.Viewport.Height / 2;
            int width = graphicsDevice.Viewport.Width / 2;
            int yOffset = 20;
            //single centred
            if (mainMenu.StartPosition != MainMenu.MenuPosition.DoubleList)
            {
                foreach (string menuItem in mainMenu.CurrentText)
                {
                    stringSize = font.MeasureString(menuItem);
                    spriteBatch.DrawString(font, menuItem, new Vector2(width - stringSize.X / 2, height - stringSize.Y / 2), Color.Red);
                    height = height + 20;
                }
            }
            else
            {
                //Dual columns
                int x = 20;
                int y = yOffset;
                int halfLength = mainMenu.CurrentText.Length / 2;
                if (mainMenu.CurrentText.Length % 2 != 0)  
                {
                    //round up mid point
                    halfLength = halfLength + 1;
                }
                //draw column 1
                for (int counter = 0; counter < halfLength; counter++)
                {
                    spriteBatch.DrawString(font, mainMenu.CurrentText[counter], new Vector2(x, y), Color.Red);
                    y = y + 20;
                }
                y = yOffset;
                x = width;
                for (int counter = halfLength; counter < mainMenu.CurrentText.Length; counter++)
                {
                    spriteBatch.DrawString(font, mainMenu.CurrentText[counter], new Vector2(x, y), Color.Red);
                    y = y + 20;
                }
            }
        }

        private void drawWizards(Wizard[] wizards)
		{
            foreach (Wizard wizard in wizards)
            {
                if (wizard.Alive)
                {
                    //Wizard is alive - draw
                    spriteBatch.Draw(wizardTexture, translate(wizard.Position), wizard.Colour);
                }
            }
		}

        private Vector2 translate(Point point)
        {
            return new Vector2((point.X - 1) * cellSize + XOffset, (point.Y - 1) * cellSize + YOffset);
        }

		private void drawTerrainElements()
		{
			throw new NotImplementedException();
		}

		private void drawCursor(Cursor cursor)
		{
            spriteBatch.Draw(cursorTexture, translate(cursor.Position), cursor.Colour);
		}

		public void update()
		{
			throw new NotImplementedException();
		}

        public int ItemsToDraw
        {
            get { return itemsToDraw; }
            set { itemsToDraw = value; }
        }
	}
}
